package com.cskaoyan.controller.system;

import com.cskaoyan.bean.MallRole;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.system.PermissionUpdateBo;
import com.cskaoyan.model.bo.system.RoleCreateBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.PageListVo;
import com.cskaoyan.model.vo.system.PermissionListVo;
import com.cskaoyan.service.system.AdminService;
import com.cskaoyan.service.system.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: project-market
 * @description: 角色管理
 * @author: Weiwei Liu
 * @create: 2021-11-30 17:15
 **/
@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    AdminService adminService;

    /**
     * 显示所有角色列表，分页查询
     */
    @RequiresPermissions("admin:role:list")
    @RequestMapping("list")
    public BaseRespVo list(PageBo pageBo, String name) {

        // 返回的是复杂的Vo
        PageListVo vo = roleService.list(pageBo, name);

        return BaseRespVo.ok(vo);

    }


    @RequestMapping("options")
    public BaseRespVo options() {

        // 返回的是简单的Vo
        PageListVo vo = roleService.options();

        return BaseRespVo.ok(vo);

    }


    @RequiresPermissions("admin:role:create")
    @PostMapping("create")
    public BaseRespVo create(@RequestBody RoleCreateBo bo) {

        // 传入简单desc和name，返回MallRole
        MallRole vo = roleService.create(bo);

        if (vo == null) return BaseRespVo.manual(640, "角色已经存在", null);

        return BaseRespVo.ok(vo);

    }


    @RequiresPermissions("admin:role:delete")
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MallRole role) {

        role.setDeleted(true);

        // 先查一下当前角色是否被管理员持有，
        boolean exist = adminService.selectRoleExistAdmin(role);

        if (!exist) {
            // 删除role
            roleService.update(role);
            return BaseRespVo.ok();
        }
        // 存在不能删除
        return BaseRespVo.manual(642, "当前角色存在管理员，不能删除", null);

    }

    @RequiresPermissions("admin:role:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody MallRole role) {

        // 传入简单desc和name，返回MallRole
        boolean status = roleService.update(role);

        if (status) return BaseRespVo.ok();

        return BaseRespVo.fail("更新失败");

    }


    /**
     * 权限详情
     *
     * @param roleId
     * @return
     */
    @RequiresPermissions("admin:role:permission:get")
    @RequestMapping("permissions")
    public BaseRespVo permissionsGet(Integer roleId) {

        PermissionListVo vo = roleService.queryPermissions(roleId);

        return BaseRespVo.ok(vo);

    }

    /**
     * 权限变更
     *
     * @param bo
     * @return
     */
    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo permissionsPost(@RequestBody PermissionUpdateBo bo) {

        PermissionListVo vo = roleService.queryAndUpdatePermissions(bo);

        return BaseRespVo.ok(vo);

    }


}
